define("vw.stat", ["echarts", "vw.map1", "bootstrap.table-cn", "jquery.sumoselect"], function(echarts) {
	"use strict";

	function vw_stat() {
		var $map, _map;
		var $chart, $table, $themeSelect, $fnChartDownload, $fnStatTableDownload;
		var _head, _body, _idIndex, _nameIndex, _themesIndexs, _enum;
		var _title, _charttype, _chartColors, _curRow, _dataIDs = [];

		this.init = function Init(mapid, chart, table, themeSelect, fnChartDownload, fnStatTableDownload) {
			$chart = chart;
			$table = table;
			$themeSelect = themeSelect;
			$fnChartDownload = fnChartDownload;
			$fnStatTableDownload = fnStatTableDownload;

			$map = $('#' + mapid);

			InitChart();
			InitTable();

			_map = $map.vw_map('destroy').vw_map({
				height: $(window).height() - 180,
				defaultLayer: "normal",
				onMouseoverMarker: function(code) {
					HighlightChart(code);
					HighlightTable(code);
				},
				onMouseoutMarker: function(code) {
					CancelHighlightChart(code);
					CancelHighlightTable(code);
				},
				onClickMarker: function(code) {
					TabStationShow(code);
				}
			});
		};

		function InitChart() {
			_chartColors = ["#3bb516", "#0577a8", "#d74005", "#ccce07", "#078c58", "#973f22"];
			_charttype = 'line';

			//			$themeSelect.SumoSelect({
			//				csvDispCount: 2,
			//				placeholder: "...",
			//				captionFormatAllSelected: ' {0} 列全部选中',
			//				captionFormat: '选中 {0} 列',
			//				triggerChangeCombined: true
			//			});
			//
			//			$themeSelect.on('change', function(e) {
			//				RenderChart(_charttype, _title);
			//			});

			$fnChartDownload.on('click', function() {
				vw_common.downloadChart($chart, _title);
			});

			$chart.on('mouseover', function(e) {
				_map.vw_map('highlightMap', _dataIDs[e.dataIndex]);
				HighlightTable(_dataIDs[e.dataIndex]);
			});

			$chart.on('mouseout', function(e) {
				_map.vw_map('cancelHighlightMap');
				CancelHighlightTable(_dataIDs[e.dataIndex]);
			});

			$chart.on('click', function(e) {});

			$(window).resize(function() {
				$chart.resize();
			});
		}

		function InitTable() {
			$fnStatTableDownload.on('click', function() {
				vw_common.downloadTable($table[0], _title);
			});

			$table.on('mouseover', ' tbody tr', function(e) {
				$(this).addClass('select-tr');
				var target = $(e.currentTarget).find('td')[_idIndex];
				if(target) {
					_map.vw_map('highlightMap', target.innerText);
					HighlightChart(target.innerText);
				}
			});

			$table.on('mouseout', ' tbody tr', function() {
				$(this).removeClass('select-tr');
				_map.vw_map('cancelHighlightMap');
				CancelHighlightChart();
			});

			$table.on('click', ' tbody tr', function(e) {
				//			var target = $(e.currentTarget).find('td')[_idIndex];
			});
		}

		this.renderer = function Renderer(title, data, charttype, maptype) {
			_title = title;
			_head = data.head.columns;
			_idIndex = data.head.id;
			_nameIndex = data.head.name;
			_themesIndexs = data.head.themes;
			_enum = data.head.enums;
			_body = data.body;
			//			_body = vw_common.quickSort(data.body, _head.length - 5);

			_charttype = charttype;

			//			$themeSelect.html('');
			//			$themeSelect[0].sumo.reload();
			//			if(_themesIndexs.length > 0) {
			//				for(var i = 0; i < _themesIndexs.length; i++) {
			//					$themeSelect[0].sumo.add(_head[_themesIndexs[i]].title);
			//				};
			//				$themeSelect[0].sumo.unSelectItem(0); //sumo的一个bug
			//				$themeSelect[0].sumo.selectItem(0);
			//			}

			RenderChart(_charttype, _title);
			RenderTable();
			
			_map.vw_map('refresh');
			if(_themesIndexs.length > 1) {
				//				_thematicmap.sVGMap(maptype, data);
				_map.vw_map('drawSVGMap', data, true, maptype);
			} else if(_themesIndexs.length == 1) {
				//				_thematicmap.pointMap(data);
				_map.vw_map('drawPoint', data, true);
			} else {}
		};

		function RenderChart(chartType, name) {
			$chart.clear();

			switch(chartType) {
				case 1:
					tab1Chart();
					break;
				case 2:
					tab2Chart();
					break;
				case 3:
					tab3Chart();
					break;
				case 4:
					tab4Chart_1();
					//tab4Chart_2();
					break;
				default:
					basicChart("bar", name);
			}
		}

		function tab1Chart() {
			var chartData = filterdata();
			var option = {
				tooltip: {
					trigger: 'axis'
				},
				grid: {
					top: '15%',
					left: '10%',
					right: '10%',
					bottom: '20%'
				},
				legend: {
					data: ['图斑总面积', '未播种面积', '套种至增加面积', '作物播种面积'],
					type: 'scroll',
				},
				xAxis: {
					data: chartData.xAxis,
					axisLabel: {
						fontSize: 10,
						color: "#999"
					}
				},
				yAxis: [{
						name: '面积(亩)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					},
					{
						name: '面积(亩)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					}
				],
				dataZoom: [{
						type: 'inside',
						start: 0,
						end: 100
					},
					{
						show: true,
						type: 'slider',
						y: '90%',
						start: 0,
						end: 100
					}
				],
				series: chartData.series
			};

			$chart.setOption(option);

			function filterdata() {
				var series = [];
				var serie = [];
				serie[0] = [];
				serie[1] = [];
				serie[2] = [];
				serie[3] = [];
				for(var i in _body) {
					serie[0][i] = _body[i].content[2];
					serie[1][i] = _body[i].content[3];
					serie[2][i] = _body[i].content[4];
					serie[3][i] = _body[i].content[5];
				}

				series.push({
					name: '图斑总面积',
					type: 'bar',
					stack: 'one',
					itemStyle: {
						color: _chartColors[0]
					},
					data: serie[0]
				});

				series.push({
					name: '未播种面积',
					type: 'bar',
					stack: 'one',
					itemStyle: {
						color: _chartColors[1]
					},
					data: serie[1]
				});

				series.push({
					name: '套种至增加面积',
					type: 'bar',
					stack: 'one',
					itemStyle: {
						color: _chartColors[2]
					},
					data: serie[2]
				});

				series.push({
					name: '作物播种面积',
					type: 'line',
					yAxisIndex: 1,
					itemStyle: {
						color: _chartColors[3]
					},
					data: serie[3]
				});

				var xAxis = [];
				for(var i in _body) {
					xAxis[i] = _body[i].content[_nameIndex];
					if(xAxis[i].length > 3) {
						xAxis[i] = xAxis[i].substr(0, 2) + "...";
					}

					_dataIDs[i] = _body[i].content[_idIndex]; //用于hightlight找对应
				}

				return {
					series: series,
					xAxis: xAxis
				};
			}
		}

		function tab2Chart() {
			var chartData = filterdata();
			var option = {
				tooltip: {
					trigger: 'axis'
				},
				grid: {
					top: '15%',
					left: '10%',
					right: '10%',
					bottom: '20%'
				},
				legend: {
					data: ['上期种植面积', '本期种植面积', '地块总面积'],
					type: 'scroll',
				},
				xAxis: {
					data: chartData.xAxis,
					axisLabel: {
						fontSize: 10,
						color: "#999"
					}
				},
				yAxis: [{
						name: '面积(亩)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					},
					{
						name: '面积(亩)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					}
				],
				dataZoom: [{
						type: 'inside',
						start: 0,
						end: 100
					},
					{
						show: true,
						type: 'slider',
						y: '90%',
						start: 0,
						end: 100
					}
				],
				series: chartData.series
			};

			$chart.setOption(option);

			function filterdata() {
				var series = [];
				var serie = [];
				serie[0] = [];
				serie[1] = [];
				serie[2] = [];
				for(var i in _body) {
					serie[0][i] = _body[i].content[2];
					serie[1][i] = _body[i].content[3];
					serie[2][i] = _body[i].content[4];
				}

				series.push({
					name: '上期种植面积',
					type: 'bar',
					stack: 'one',
					itemStyle: {
						color: _chartColors[0]
					},
					data: serie[0]
				});

				series.push({
					name: '本期种植面积',
					type: 'bar',
					stack: 'one',
					itemStyle: {
						color: _chartColors[1]
					},
					data: serie[1]
				});

				series.push({
					name: '地块总面积',
					type: 'line',
					yAxisIndex: 1,
					itemStyle: {
						color: _chartColors[2]
					},
					data: serie[2]
				});

				var xAxis = [];
				for(var i in _body) {
					xAxis[i] = _body[i].content[_nameIndex];
					if(xAxis[i].length > 3) {
						xAxis[i] = xAxis[i].substr(0, 2) + "...";
					}

					_dataIDs[i] = _body[i].content[_idIndex]; //用于hightlight找对应
				}

				return {
					series: series,
					xAxis: xAxis
				};
			}
		}

		function tab3Chart() {
			var chartData = filterdata();
			var option = {
				tooltip: {
					trigger: 'axis'
				},
				grid: {
					top: '15%',
					left: '10%',
					right: '10%',
					bottom: '20%'
				},
				legend: {
					data: ['上年环比', '均值', '方差'],
					type: 'scroll',
				},
				xAxis: {
					data: chartData.xAxis,
					axisLabel: {
						fontSize: 10,
						color: "#999"
					}
				},
				yAxis: [{
						name: '面积(亩)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					},
					{
						name: '面积(亩)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					}
				],
				dataZoom: [{
						type: 'inside',
						start: 0,
						end: 100
					},
					{
						show: true,
						type: 'slider',
						y: '90%',
						start: 0,
						end: 100
					}
				],
				series: chartData.series
			};

			$chart.setOption(option);

			function filterdata() {
				var series = [];

				function data_s1() {
					var serie = [];
					var cols = _head.length;
					for(var i in _body) {
						serie[i] = [];
						serie[i][0] = _body[i].content[cols - 6];
						serie[i][1] = _body[i].content[cols - 5];
						serie[i][2] = _body[i].content[cols - 3];
						serie[i][3] = _body[i].content[cols - 4];
					}
					return serie;
				}

				series.push({
					name: "上年环比",
					type: 'candlestick',
					itemStyle: {
						normal: {
							color: '#ec0000',
							color0: '#00da3c',
							borderColor: '#8A0000',
							borderColor0: '#008F28'
						}
					},
					data: data_s1()
				});

				function data_s2() {
					var serie = [];
					var cols = _head.length;
					for(var i in _body) {
						serie[i] = _body[i].content[cols - 2];
					}
					return serie;
				}

				series.push({
					name: "均值",
					type: 'line',
					itemStyle: {
						color: _chartColors[3],
					},
					data: data_s2()
				});

				function data_s3() {
					var serie = [];
					var cols = _head.length;
					for(var i in _body) {
						serie[i] = _body[i].content[cols - 1];
					}
					return serie;
				}

				series.push({
					name: "方差",
					type: 'line',
					yAxisIndex: 1,
					itemStyle: {
						color: _chartColors[2],
					},
					data: data_s3()
				});

				var xAxis = [];
				for(var i in _body) {
					xAxis[i] = _body[i].content[_nameIndex];
					if(xAxis[i].length > 3) {
						xAxis[i] = xAxis[i].substr(0, 2) + "...";
					}

					_dataIDs[i] = _body[i].content[_idIndex]; //用于hightlight找对应
				}

				return {
					series: series,
					xAxis: xAxis
				};
			}
		}

		function tab4Chart_2() {
			var chartData = filterdata();
			var option = {
				tooltip: {
					trigger: 'axis'
				},
				grid: {
					top: '15%',
					left: '10%',
					right: '10%',
					bottom: '20%'
				},
				legend: {
					data: ['样方内点数', '样方外点数', '样方内点比例'],
					type: 'scroll',
				},
				xAxis: {
					data: chartData.xAxis,
					axisLabel: {
						fontSize: 10,
						color: "#999"
					}
				},
				yAxis: [{
						name: '数量(个)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					},
					{
						name: '比例(%)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					}
				],
				dataZoom: [{
						type: 'inside',
						start: 0,
						end: 100
					},
					{
						show: true,
						type: 'slider',
						y: '90%',
						start: 0,
						end: 100
					}
				],
				series: chartData.series
			};

			$chart.setOption(option);

			function filterdata() {
				var series = [];

				function data_s1() {
					var serie = [];
					for(var i in _body) {
						serie[i] = _body[i].content[2];
					}
					return serie;
				}

				series.push({
					name: "样方内点数",
					type: 'bar',
					stack: 'one',
					itemStyle: {
						color: _chartColors[0]
					},
					data: data_s1()
				});

				function data_s2() {
					var serie = [];
					for(var i in _body) {
						serie[i] = _body[i].content[3];
					}
					return serie;
				}

				series.push({
					name: "样方外点数",
					type: 'bar',
					stack: 'one',
					itemStyle: {
						color: _chartColors[1]
					},
					data: data_s2()
				});

				function data_s3() {
					var serie = [];
					for(var i in _body) {
						serie[i] = _body[i].content[4];
					}
					return serie;
				}

				series.push({
					name: "样方内点比例",
					type: 'line',
					yAxisIndex: 1,
					itemStyle: {
						color: _chartColors[2]
					},
					data: data_s3()
				});

				var xAxis = [];
				for(var i in _body) {
					xAxis[i] = _body[i].content[_nameIndex];
					if(xAxis[i].length > 3) {
						xAxis[i] = xAxis[i].substr(0, 2) + "...";
					}

					_dataIDs[i] = _body[i].content[_idIndex]; //用于hightlight找对应
				}

				return {
					series: series,
					xAxis: xAxis
				};
			}
		}

		function tab4Chart_1() {
			var chartData = filterdata();
			var option = {
				tooltip: {
					trigger: 'axis'
				},
				grid: {
					top: '15%',
					left: '10%',
					right: '10%',
					bottom: '20%'
				},
				legend: {
					data: ['调查时间', '上报时间', '地块面积'],
					type: 'scroll',
				},
				xAxis: {
					data: chartData.xAxis,
					axisLabel: {
						fontSize: 10,
						color: "#999"
					}
				},
				yAxis: [{
						name: '日',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					},
					{
						name: '面积(亩)',
						type: 'value',
						axisLabel: {
							fontSize: 10,
							color: "#999"
						}
					}
				],
				dataZoom: [{
						type: 'inside',
						start: 0,
						end: 100
					},
					{
						show: true,
						type: 'slider',
						y: '90%',
						start: 0,
						end: 100
					}
				],
				series: chartData.series
			};

			$chart.setOption(option);

			function filterdata() {
				var series = [];

				function data_s1() {
					var serie = [];
					for(var i in _body) {
						serie[i] = _body[i].content[5];
					}
					return serie;
				}

				series.push({
					name: "调查时间",
					type: 'bar',
					stack: 'one',
					itemStyle: {
						normal: {
							barBorderColor: 'rgba(0,0,0,0)',
							color: 'rgba(0,0,0,0)'
						},
						emphasis: {
							barBorderColor: 'rgba(0,0,0,0)',
							color: 'rgba(0,0,0,0)'
						}
					},
					data: data_s1()
				});

				function data_s2() {
					var serie = [];
					for(var i in _body) {
						serie[i] = _body[i].content[6];
					}
					return serie;
				}

				series.push({
					name: "上报时间",
					type: 'bar',
					stack: 'one',
					label: {
						normal: {
							show: true,
							position: 'inside'
						}
					},
					itemStyle: {
						color: _chartColors[1]
					},
					data: data_s2()
				});

				function data_s3() {
					var serie = [];
					for(var i in _body) {
						serie[i] = _body[i].content[7];
					}
					return serie;
				}

				series.push({
					name: "地块面积",
					type: 'line',
					yAxisIndex: 1,
					itemStyle: {
						color: _chartColors[2]
					},
					data: data_s3()
				});

				var xAxis = [];
				for(var i in _body) {
					xAxis[i] = _body[i].content[_nameIndex];
					if(xAxis[i].length > 3) {
						xAxis[i] = xAxis[i].substr(0, 2) + "...";
					}

					_dataIDs[i] = _body[i].content[_idIndex]; //用于hightlight找对应
				}

				return {
					series: series,
					xAxis: xAxis
				};
			}
		}

		function basicChart(chartType, name) {
			var selThemes = $themeSelect[0].sumo.getSelStr().split(",");
			$chart.clear();
			if(selThemes == "") {
				return;
			}
			var chartData = filterdata(selThemes);
			var option = {
				title: {
					text: name,
					textStyle: {
						fontSize: 16
					},
					x: 'center'
				},
				tooltip: {
					trigger: 'axis'
				},
				grid: {
					left: 30,
					right: 10,
					top: 40,
				},
				legend: {
					data: selThemes,
					type: 'scroll',
					bottom: 5,
				},
				xAxis: {
					data: chartData.xAxis,
					axisLabel: {
						fontSize: 10,
						color: "#999"
					}
				},
				yAxis: {
					axisLabel: {
						fontSize: 10,
						color: "#999"
					}
				},
				series: chartData.series
			};
			$chart.setOption(option);

			function filterdata() {
				var series = [];
				for(var i in _themesIndexs) {
					var thisCol = _head[_themesIndexs[i]].title;
					if(selThemes.indexOf(thisCol) != -1) {
						var serie = [];
						for(var j in _body) {
							serie[j] = _body[j].content[_themesIndexs[i]];
						}

						series.push({
							name: thisCol,
							type: chartType,
							itemStyle: {
								color: _chartColors[i],
							},
							data: serie
						});
					}
				}

				var xAxis = [];
				for(var i in _body) {
					xAxis[i] = _body[i].content[_nameIndex];
					if(xAxis[i].length > 3) {
						xAxis[i] = xAxis[i].substr(0, 2) + "...";
					}

					_dataIDs[i] = _body[i].content[_idIndex]; //用于hightlight找对应
				}

				return {
					series: series,
					xAxis: xAxis
				}
			}
		}

		function RenderTable() {
			var rows = [];
			for(var i = 0; i < _body.length; i++) {
				rows[i] = _body[i].content;
			};

			for(var i = 0; i < _head.length; i++) {
				_head[i].sortable = true;
				_head[i].align = 'center';
			};

			$table.bootstrapTable('destroy').bootstrapTable({
				"data": rows,
				"columns": _head,
				"height": 220,
			});
		}

		function HighlightChart(index) {
			var dataIndex = -1;
			for(var i in _dataIDs) {
				if(_dataIDs[i] == index) {
					dataIndex = i;
					break;
				}
			}
			$chart.dispatchAction({
				type: 'showTip',
				seriesIndex: 0,
				dataIndex: dataIndex,
			})
		};

		function CancelHighlightChart(index) {
			$chart.dispatchAction({
				type: 'hideTip'
			})
		}

		function HighlightTable(index) {
			var trs = $table.find("tbody tr");
			for(var i = 0; i < trs.length; i++) {
				var tds = $(trs[i]).find('td');
				for(var j = 0; j < tds.length; j++) {
					if($(tds[j]).html() == index) {
						$(trs[i]).addClass('select-tr');
						$table.parent().scrollTop($(trs[i]).height() * i);
						$table.parent().scrollLeft(0);
						_curRow = trs[i];
						break;
					}
				}
			}
		}

		function CancelHighlightTable(index) {
			if(_curRow != undefined) {
				$(_curRow).removeClass('select-tr');
			}
		}
	}

	return function() {
		return new vw_stat();
	}
})